package com.example.allplan.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.allplan.Fragment.JiHuaFragment;
import com.example.allplan.ItemTouchHelper.JiHuaBaoTouchCallback;
import com.example.allplan.JavaBean.JiHuaBaoJavaBean;
import com.example.allplan.JavaBean.JiHuaJavaBean;
import com.example.allplan.MyApplication;
import com.example.allplan.MyView.MyLinearLayout;
import com.example.allplan.R;
import com.example.allplan.RecyclerAdapter.JiHuaBaoRecyclerAdapter;

import java.util.ArrayList;
import java.util.Collections;

public class GuanLiJiHuaActivity extends BaseActivity{
    MyLinearLayout mgen;
    RecyclerView guan_li_ji_hua_recyclerview;
    ArrayList<JiHuaBaoJavaBean> mlist;
    MyApplication mmyapp;
    JiHuaFragment mmyfragment;
    ImageView bei_jing_tu;
    AppCompatActivity mactivity;
    JiHuaBaoRecyclerAdapter madapter;
    Toolbar mtoolbar;
    int ji_hua_id;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bao_xuan_ze_activity_layout);
        init();
    }

    void reload() {
        mlist = mmyapp.mSuoYouJiHuaBaoSQCaoZuo.reloadAllJiHuaBao();
        mlist.remove(1);
        int yichu=-1;
        int suoshuid=mmyapp.mSuoYouJiHuaSQCaoZuo.reloadByIdJiHua(ji_hua_id).getSuo_shu_ji_hua_bao_id();
        for(int i=0;i<mlist.size();i++){
            if(mlist.get(i).getId()==suoshuid){
                yichu=i;
                break;
            }
        }
        if(yichu!=-1) mlist.remove(yichu);
        Collections.sort(mlist);
    }
    JiHuaBaoJavaBean  reloadonebyid(int n){
        JiHuaBaoJavaBean mo= mmyapp.mSuoYouJiHuaBaoSQCaoZuo.reloadByIdJiHuaBao(n);
        Toast.makeText(mmyapp, mo.getBao_ming(), Toast.LENGTH_SHORT).show();
        return mo;
    }

    void init() {
        ji_hua_id=getIntent().getIntExtra("ji_hua_id",-1);
        mgen=findViewById(R.id.guan_li_ji_hua_gen_mylinearlayout);
        mmyapp = MyApplication.getInstance();
        mtoolbar=findViewById(R.id.guan_li_ji_hua_toolbar);
        setSupportActionBar(mtoolbar);
        bei_jing_tu = findViewById(R.id.guan_li_ji_hua_imageview);
        reload();
        if (mlist.size() == 0) {
            bei_jing_tu.setVisibility(View.VISIBLE);
        }
        Collections.sort(mlist);
        guan_li_ji_hua_recyclerview = findViewById(R.id.guan_li_ji_hua_recyclerview);
        madapter = new JiHuaBaoRecyclerAdapter(mlist, new JiHuaBaoRecyclerAdapter.onItemClickListener() {
            @Override
            public void onClick(int pos, Context mcon) {
                JiHuaJavaBean temp=mmyapp.mSuoYouJiHuaSQCaoZuo.reloadByIdJiHua(ji_hua_id);
                JiHuaBaoJavaBean baotemp=mmyapp.mSuoYouJiHuaBaoSQCaoZuo.reloadByIdJiHuaBao(temp.getSuo_shu_ji_hua_bao_id());
                baotemp.setJi_hua_shu(baotemp.getJi_hua_shu()-1);
                temp.setYuan_suo_shu_id(temp.getSuo_shu_ji_hua_bao_id());
                temp.setSuo_shu_ji_hua_bao_id(mlist.get(pos).getId());
                temp.setPai_xu_you_biao(mlist.get(pos).getJi_hua_shu());
                mlist.get(pos).setJi_hua_shu(mlist.get(pos).getJi_hua_shu()+1);
                mmyapp.mSuoYouJiHuaSQCaoZuo.update_one(temp);
                mmyapp.mSuoYouJiHuaBaoSQCaoZuo.update_one(mlist.get(pos));
                mmyapp.mSuoYouJiHuaBaoSQCaoZuo.update_one(baotemp);
                Intent min=new Intent();
                min.putExtra("ji_hua_id",ji_hua_id);
                setResult(72,min);
                finish();
            }
        }, this);
        guan_li_ji_hua_recyclerview.setAdapter(madapter);
        guan_li_ji_hua_recyclerview.setLayoutManager(new LinearLayoutManager(this));


    }



    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                return true;

        }
        return true;
    }


}

